<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Promise API - all</title>
</head>
<body>
    <script>




       function sleep(ms) {
            var start = Date.now()
            while(Date.now() - start <= ms ) ; 
       }


       function sendAJAX(url){
            return new Promise((resolve, reject) => {
                const xhr = new XMLHttpRequest();
                xhr.responseType = 'json';
                xhr.open("GET", url);
                xhr.send();
                //处理结果
                xhr.onreadystatechange = function(){
                    if(xhr.readyState === 4){
                        //判断成功
                        if(xhr.status >= 200 && xhr.status < 300){
                            //成功的结果
                            resolve(xhr.response);
                        }else{
                            reject(xhr.status);
                        }
                    }
                }
            });
        }
    
        


        let p1 = sendAJAX('http://localhost/ajax/ajax3.php?type=p1&t=0'); 


        // let p2 = Promise.resolve('Success');
        let p2 = sendAJAX('http://localhost/ajax/ajax3.php?type=p2');
        // let p3 = sendAJAX('https://api.apiopen.top/getJoke')
        let p3 = sendAJAX('http://localhost/ajax/ajax3.php?type=p3&t=1  ')
        
        console.time('aa')
        const result = Promise.all([p2, p1, p3]);

        console.log(result);
        result.then(value => {
            console.log('value', value) ;
             console.timeEnd('aa')

        }, error => {
            console.log('error => ', error) ;
        })
    </script>
</body>
</html>